package com.netty.utils;

import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.io.File;
import java.io.FileInputStream;


/**
 * 
 *@author Clare
 *@Company 
 *
 *Print file by javax
 * 
 */

public class PrintPdfJavax {

    public static void main(String[] args) {

            File file = new File("C:\\Users\\Administrator\\Desktop\\Netty实战.pdf"); // 获取选择的文件

            // 构建打印请求属性集

            HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

            // 设置打印格式，因为未确定类型，所以选择autosense

            DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;

            // 查找所有的可用的打印服务

            PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
            if (printService.length>0){
                for(int i=0;i<printService.length;i++){
                    System.out.println(printService[i]);
                }
                // 定位默认的打印服务
                PrintService service= PrintServiceLookup.lookupDefaultPrintService();
                for (int i = 0; i < printService.length; i++) {
                    if (printService[i].getName().contains("FinePrint")){
                        service = printService[i];
                    }

                }

                if(printService.length>0){
                    System.out.println("-------------------Choose Printer-------------------");
                    System.out.println(printService[0]);
                    //指定使用 Microsoft XPS Document Writer

                    //获取打印机属性
                    AttributeSet attributes = service.getAttributes();
                    for (Attribute a : attributes.toArray()) {
                        String name = a.getName();
                        String value = attributes.get(a.getClass()).toString();
                        System.out.println(name + " : " + value);
                    }
                    // 显示打印对话框

                    //PrintService service = ServiceUI.printDialog(null, 200, 200, printService,

                    //    service1, flavor, pras);

                    if (service != null) {

                        try {
                            System.out.println("Begin Print PDF: "+file.getName());
                            DocPrintJob job = service.createPrintJob(); // 创建打印作业

                            FileInputStream fis = new FileInputStream(file); // 构造待打印的文件流

                            DocAttributeSet das = new HashDocAttributeSet();
                            //请求一个彩色打印机
                            pras.add(Chromaticity.COLOR);

                            //请求横向模式
                            pras.add(OrientationRequested.LANDSCAPE);

                            //美国Letter大小的纸质属性
                            pras.add(MediaSizeName.NA_LETTER);

                            // European A4 paper
                            pras.add(MediaSizeName.ISO_A4);

                            //请求装订
                            pras.add(Finishings.STAPLE);

                            //整理多个副本
                            pras.add(SheetCollate.COLLATED);

                            //请求双面
                            pras.add(Sides.DUPLEX);

                            // 2页到一个工作表
                            pras.add(new NumberUp(2));

                            //多少个副本
                            pras.add(new Copies(2));

                            Doc doc = new SimpleDoc(fis, flavor, das);

                            job.print(doc, pras);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
            }
        }   else {
                System.out.println("没有可用打印机");
            }
    }
}